//
//  LBOrderController.m
//  LBOrder
//
//  Created by lc on 2016/11/19.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "LBOrderController.h"
#import "TableViewCell.h"
#import "OrderModel.h"
#import "LBMeOrderDetailController.h"
#import "LBPlusMinusTool.h"
#import "LBSuperMarketProducts.h"

static NSString *ID = @"cell_id";

@interface LBOrderController ()
//模型数组
@property(nonatomic,strong)NSArray<OrderModel *> *array;
@end

@implementation LBOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.view.backgroundColor = [UIColor whiteColor];
}

-(void)viewWillAppear:(BOOL)animated {
    //屏蔽统一navigationBar的样式，自定义样式
    [self.navigationController.navigationBar setTintColor:[UIColor grayColor]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}

-(instancetype)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        self.array = [NSArray array];
        
        
        [self loadData];

        [self setupUI];
    }
    
    return self;

}

-(void)loadData {
    
    //创建单例
    NSMutableArray *arrayImage = [NSMutableArray array];
    LBPlusMinusTool  *tool = [LBPlusMinusTool sharedPlusMinusTool];
    NSArray *arrayP = tool.selectedProductsModel;
    [arrayP enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop){
        LBSuperMarketProducts *product = arrayP[idx];
        [arrayImage addObject:product.img];
    }];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    OrderModel *model = [[OrderModel alloc] init];
    
    model.images = arrayImage;
    
    model.lastUpdateTime = @"2016-11-20 11:23";
    model.textStatus = @"完成";
    
    model.goods_nums = [tool allCount];
    model.real_price = [NSNumber numberWithFloat: [tool selectedProductPrice]];
    
    //[arrayM addObject:model];
    [arrayM addObject:model];
    
    _array = arrayM.copy;
    
    [self.tableView reloadData];
    
}

-(void)setupUI {
    
    //titl头
         self.navigationItem.title = @"我的订单";
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:ID];
    
    //自动预估行高
   // self.tableView.estimatedRowHeight = 125;

   // self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.rowHeight = 125;
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.model = self.array[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LBMeOrderDetailController *orderVc = [[LBMeOrderDetailController alloc] init];
    
    [self.navigationController pushViewController:orderVc animated:YES];
}


//改变组间距
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
